package com.rednote.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rednote.common.Result;
import com.rednote.common.UserContextHolder;
import com.rednote.entity.UserLikes;
import com.rednote.entity.vo.PostCardVO;
import com.rednote.service.RatingService;
import com.rednote.service.UserCollectService;
import com.rednote.service.UserLikesService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 用户收藏记录表(UserCollects)控制层
 *
 * @author wzy
 */
@RestController
@RequestMapping("/userCollects")
public class UserCollectsController {
    @Resource
    private UserCollectService userCollectService;
    @Resource
    private RatingService ratingService;
    /**
     * 收藏帖子
     * @param id 帖子ID
     * @return 收藏结果
     */
    @PostMapping("/posts")
    public Result<?> collectPost(@RequestParam Integer id) {
        try {
            userCollectService.collect(id);
            int userId = UserContextHolder.getUserId();
            ratingService.updateUserPostRating(userId, id, "FAVORITE"); // 或"FAVORITE"、"VIEW"、"COMMENT"

            return Result.success("收藏成功");
        } catch (Exception e) {
            return Result.fail("收藏失败: " + e.getMessage());
        }
    }



    /**
     * 取消收藏帖子
     * @param id 帖子ID
     * @return 取消收藏结果
     */
    @DeleteMapping("/posts")
    public Result<?> cancelCollectPost(@RequestParam Integer id) {
        try {
            userCollectService.cancelCollect(id);
            return Result.success("取消收藏成功");
        } catch (Exception e) {
            return Result.fail("取消收藏失败: " + e.getMessage());
        }
    }

    /**
     * 收藏帖子列表获取
     * @param page 页码
     * @param limit 每页大小
     *
     */
    @GetMapping("/list")
    public Result<?> getCollectList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        try {
            Page<PostCardVO> result = userCollectService.getCollectList(page, limit);
            return Result.success("获取成功", result);
        } catch (Exception e) {
            return Result.fail("获取失败: " + e.getMessage());
        }
    }
}
